package com.java.api;

import com.java.config.result.Result;
import com.java.model.user.Patient;
import com.java.service.PatientService;
import com.java.utils.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    //查询人员列表
    @GetMapping("/auth/findAll")
    public Result findAll(HttpServletRequest request){
        //根据请求头获取token 或找到userid
        Long userId = AuthContextHolder.getUserId(request);
        //根据userid查询出所有的人员列表
        List<Patient> patientList = patientService.findList(userId);
        return Result.ok(patientList);
    }

    //添加就诊人
    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        //设置userId 因为是userId的联系人 对应
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    //根据id编号查询人员信息
    @GetMapping("/auth/get/{id}")
    public Result getPatientById(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    //修改就诊人
    @PutMapping("/auth/update")
    public Result updatePatientById(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    //删除就诊人
    @DeleteMapping("/auth/delete/{id}")
    public Result updatePatientById(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }


    //"获取就诊人"
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id){
        return patientService.getPatientById(id);
    }
}
